package com.jasonjan.jiance.global;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Environment;
import android.support.multidex.MultiDexApplication;

import com.jasonjan.jiance.BuildConfig;
import com.jasonjan.jiance.R;
import com.jasonjan.jiance.util.CrashHandlerUtil;
import com.jasonjan.jiance.util.T;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.io.File;

/**
 * Created by JasonJan on 2017/11/30.
 */

public class InitApp extends MultiDexApplication {
    public static Context AppContext;

    @Override
    public void onCreate(){
        super.onCreate();

        AppContext=getApplicationContext();

        //初始化主题
        initTheme();
        //错误日志分析
        CrashHandlerUtil.getInstance().init(AppContext);

        if(BuildConfig.DEBUG){
            SDKManager.initStetho(AppContext);
        }

        T.register(AppContext);
    }

    private void initTheme(){

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        initImageLoader();
    }

    /** 初始化imageLoader */
    private void initImageLoader() {
        DisplayImageOptions options = new DisplayImageOptions.Builder().showImageForEmptyUri(R.mipmap.icon_empty)
                .showImageOnFail(R.mipmap.icon_empty).showImageOnLoading(R.mipmap.icon_empty).cacheInMemory(true)
                .cacheOnDisc(true).build();

        File cacheDir = new File(Environment.getExternalStorageDirectory().toString() + File.separator + "jiance" + File.separator + "Images"+File.separator);
        ImageLoaderConfiguration imageconfig = new ImageLoaderConfiguration.Builder(this)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .discCacheSize(50 * 1024 * 1024)
                .discCacheFileCount(200)
                .discCache(new UnlimitedDiscCache(cacheDir))
                .discCacheFileNameGenerator(new Md5FileNameGenerator())
                .defaultDisplayImageOptions(options).build();

        ImageLoader.getInstance().init(imageconfig);

    }
}
